<?php

/**
 * Classe Forum: gestisce un singolo forum e le sue informazioni, ogni forum ha tanti topic e un topic è legato ad un solo forum
 *
 * @author Stefano Poncini
 */
class Forum {
    public $id_forum;
    public $name;
    public $desc;
    public $creation_date;
    public $creator;
    public $status;
    public $type;
    public $category;
    public $visible;
    public $topics = array();
    public $num_topics;
    
    /**
     * Costruttore della classe Forum: instanzia una nuova istanza e valorizza le variabili globali
     * @param int $id_forum
     * @param string $name
     * @param string $desc
     * @param date $creation_date
     * @param string $creator
     * @param int $status
     * @param int $type
     * @param string $category
     * @param int $visible
     */
    public function __construct($id_forum, $name, $desc, $creation_date, $creator, $status, $type, $category, $visible) {
        $this->id_forum = $id_forum;
        $this->name = $name;
        $this->desc = $desc;
        $this->creation_date = $creation_date;
        $this->creator = $creator;
        $this->status = $status;
        $this->type = $type;
        $this->category = $category;
        $this->visible = $visible;
    }
    
    /**
     * Metodo getTopics: ritorna tutti i topic legati ad un forum
     * 
     * @return \Topic
     */
    public function getTopics(){
        $sql = "SELECT * FROM topic where forum_id = ? and visible = 1";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($this->id_forum));

        while ($topic = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $this->topics[] = new Topic($topic['id_topic'], $topic['name'], $topic['status'], $topic['creation_date'], $topic['creator'], $topic['type'], $topic['content'], $topic['views'], $topic['forum_id'], $topic['visible']);
        }
        
        return $this->topics;
    }
    
    /**
     * 
     * @param int $id
     * @return boolean|\Forum
     */
    public static function getForum($id){
        $sql = "SELECT *, count(*) as num FROM forum where id_forum = ? ";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($id));

        $forum = $stmt->fetch();

        if ($forum['num'] != 1) {
            return false;
        } else {
            // Carico i dati dell'utente nelle variabili di classe

            $forum = new Forum($forum['id_forum'], $forum['name'], $forum['desc'], $forum['creation_date'], $forum['creator'], $forum['status'], $forum['type'], $forum['category'], $forum['visible']);

            return $forum;
        }
    }
    
    /**
     * Metodo getNrTopic: ritorna il numero di topic in un determinato forum
     * 
     * @return int
     */
    public function getNrTopic(){
        $sql = "SELECT count(*) as num FROM topic where forum_id = ? ";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($this->id_forum));

        $nrtopics = $stmt->fetch();
    
        return $nrtopics['num'];
    }
    
    /**
     * Metodo getNrPosts: ritorna il numero di post in tutti i topic di un determinato forum
     * 
     * @return int
     */
    public function getNrPosts(){
        $sql = "select count(*) as num from post, topic where topic_id = id_topic and forum_id = ? ";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($this->id_forum));

        $nrtopics = $stmt->fetch();
    
        return $nrtopics['num'];
    }

    /**
     * Metodo getLastMessageDate: ritorna la data dell'ultimo messaggio nel forum
     * @return boolean
     */
    public function getLastMessageDate(){
        $sql = "select post.creation_date as date from post, topic where id_topic = topic_id and forum_id = ? order by post.creation_date desc limit 1";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($this->id_forum));

        $res_post = $stmt->fetch();

        if(isset($res_post['date'])){
            return $res_post['date'];
        }else{
            return false;
        }
    }
    
    /**
     * Metodo getLastMessageCreator: ritorna l'utente che ha mandato l'ultimo messaggio nel forum
     * @return string
     */
    public function getLastMessageCreator(){
        $sql = "select post.creator as creator from post, topic where id_topic = topic_id and forum_id = ? order by post.creation_date desc limit 1";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($this->id_forum));

        $res_post = $stmt->fetch();

        if(isset($res_post['creator'])){
            return $res_post['creator'];
        }else{
            return "nessuno";
        }
    }
    
    /**
     * 
     * @param type $name
     * @param type $desc
     * @param type $user
     * @return boolean
     */
    public static function newForum($name, $desc, $cat, $user){
        try {
            $sql = "INSERT INTO forum(`id_forum`,`name`,`desc`,`status`,`type`,`creator`,`creation_date`,`category`,`visible`) VALUES(?,?,?,?,?,?,?,?,?)";

            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array(null, $name, $desc, 1,1,$user, date("Y-m-d H:i:s", time()), $cat, 1));

            return true;
        } catch (PDOException $e) {

            return false;
        }
    }
    
    public static function removeForum($id){
        try {
            $sql = "UPDATE forum SET visible = ? WHERE id_forum = ?";
            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array(0,$id));
            return true;
        } catch (PDOException $e) {

            return false;
        }
        
    }
    
    public static function moveForum($forum_name, $category){
        try {
            $sql = "UPDATE forum SET category = ? WHERE name = ?";
            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array($category,$forum_name));
            return true;
        } catch (PDOException $e) {

            return false;
        }
        
    }
}

?>
